﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Serialization;

namespace Meraz.Core.Model
{
    [Serializable]
    [DataContract]
    public class Tweet
    {
        public Tweet()
        {
            Mentions = new List<TwitterMention>();
            Tags = new List<TwitterTag>();
            Links = new List<TwitterLink>();
            InReplyToScreenName = string.Empty;
            InReplyToStatusID = 0;
            InReplyToUserID = 0;
        }
        [DataMember]
        public long ID { get; set; }
        [DataMember]
        public string Identifier { get { return ID.ToString(); } set { ID = Int64.Parse(value); } }
        [DataMember]
        public string Text { get; set; }
        [DataMember]
        public DateTime Pubdate { get; set; }
        [DataMember]
        public long PubdateTicks { get { return Pubdate.Ticks; } set { Pubdate = new DateTime(value); } }
        [DataMember]
        public long UserID { get; set; }

        public string Username { get; set; }


        [DataMember]
        public string InReplyToScreenName { get; set; }
        [DataMember]
        public long InReplyToStatusID { get; set; }
        [DataMember]
        public long InReplyToUserID { get; set; }

        [DataMember]
        public bool IsClarification { get; set; }
        [DataMember]
        public bool IsGeneral { get; set; }
        [DataMember]
        public bool IsLive { get; set; }
        [DataMember]
        public bool IsQuestion { get; set; }
        [DataMember]
        public bool IsRetweeted { get; set; }
        [DataMember]
        public string Genre
        {
            get { return this.GenreType.ToString(); }
            set
            {
                if (string.IsNullOrWhiteSpace(value))
                    this.GenreType = TwitterGenreType.Other;
                else
                {
                    try
                    {
                        this.GenreType = (TwitterGenreType)Enum.Parse(typeof(TwitterGenreType), value);
                    }
                    catch
                    {
                        this.GenreType = TwitterGenreType.Other;
                    }
                }
            }
        }
        public TwitterGenreType GenreType { get; set; }
        [DataMember]
        public virtual List<TwitterMention> Mentions { get; set; }
        [DataMember]
        public virtual List<TwitterTag> Tags { get; set; }
        [DataMember]
        public virtual List<TwitterLink> Links { get; set; }

        [DataMember]
        public string Status
        {
            get { return this.StatusType.ToString(); }
            set
            {
                if (string.IsNullOrWhiteSpace(value))
                {
                    this.StatusType = StatusType.New;
                }
                else
                {
                    try
                    {
                        this.StatusType = (StatusType)Enum.Parse(typeof(StatusType), value);
                    }
                    catch
                    {
                        this.StatusType = StatusType.New;
                    }
                }
            }

        }
        public StatusType StatusType { get; set; }


        [DataMember]
        public virtual TwitterClient Client { get; set; }

        public int ClientID { get; set; }


        public bool HasMentions { get { return Mentions != null && Mentions.Count > 0; } }
        public bool HasTags { get { return Tags != null && Tags.Count > 0; } }
        public bool HasLinks { get { return Links != null && Links.Count > 0; } }
        public bool InRange(DateTime start, DateTime end)
        {
            bool result = this.Pubdate >= start && this.Pubdate <= end;
            return result;
        }
        public bool InRange(long start, long end)
        {
            bool result = PubdateTicks >= start && PubdateTicks <= end;
            return result;
        }
    }
}
